//
//  currentDataSource.m
//  JxCarios
//
//  Created by xcq on 15/1/25.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "CurrentDataSource.h"

#define WEAHTERURL(value) [NSString  stringWithFormat:@"http://toy.weather.com.cn/SearchBox/searchBox?callback=jsonp1343396048201&_=1343396067262&language=zh&keyword=%@",value]


@implementation CurrentDataSource

- (void)userLogin:(void (^)(id result))callback andFail:(void (^)(id))failCallback{
    
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setObject:_userName forKey:@"loginname"];
    
    [params setObject:_passWord forKey:@"pwd"];
    [params setObject:@"" forKey:@"token"];
    [params setObject:@"ios" forKey:@"apptype"];
    
    [self msg:@"imilogin" parameters:params requestIdentifier:1001 success:^(id result) {
        callback(result);
    }fail:^(id result) {
        failCallback(result);
    }];
}

-(void)registers:(void (^)(id result))callback withStatus:(NSInteger)status andFail:(void (^)(id))failCallback{
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setObject:_mobile forKey:@"mobile"];
    [params setObject:_passWord forKey:@"password"];
    [params setObject:_rand forKey:@"rand"];
    if (status == 1) {
        [self msg:@"imiregister" parameters:params requestIdentifier:1002 success:^(id result) {
            callback(result);
        }fail:^(id result) {
            failCallback(result);
        }];
    }else{
        [self msg:@"imiseachpwd" parameters:params requestIdentifier:1004 success:^(id result) {
            callback(result);
        }fail:^(id result) {
            failCallback(result);
        }];
    }
}

/*
 * type	imiupdatepwd	需要请求的接口名称
 mobile	13458582828	登录名（手机号）
 oldpwd	123456	旧密码
 newpwd	654321	新密码
 */
-(void)changePassWord:(void (^)(id))callback andFail:(void (^)(id))failCallback {
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:_oldPwd forKey:@"oldpwd"];
    [param setObject:_xinPwd forKey:@"newpwd"];
    [param setObject:[Common currentPhoneNum] forKey:@"mobile"];
    
    [self msg:@"imiupdatepwd" parameters:param requestIdentifier:1010 success:^(id result) {
        callback(result);
    }fail:^(id result) {
        failCallback(result);
    }];
}

-(void)getRand:(SIMPLE_BLOCK_MAKE(id result))callback andFail:(SIMPLE_BLOCK_MAKE(id result))failCallback  withStatus:(NSInteger)status {
    NSMutableDictionary *params = [NSMutableDictionary  new];
    [params setObject:_userName forKey:@"mobile"];
    if (status == 1) {//注册
        [self msg:@"imiregistermsg" parameters:params requestIdentifier:1003 success:^(id result) {
            callback(result);
        }fail:^(id result) {
            failCallback(result);
        }];
        
    }else{//找回密码
        [self msg:@"imiseachpwdmsg" parameters:params requestIdentifier:1005 success:^(id result) {
            callback(result);
        }fail:^(id result) {
            failCallback(result);

        }];
    }
   
}

- (void)getHomeActivityPic:(void (^)(id))callback andFail:(void (^)(id))failCallback{
    NSMutableDictionary *dic = [NSMutableDictionary new];
    if([Common currenLoginStatus]){
        [dic setObject:[Common currentUserId] != nil ? [Common currentUserId] : @"-1" forKey:@"memberid"];
    }else{
        [dic setObject:@"-1" forKey:@"memberid"];
    }
    
    [self msg:@"imipicture" parameters:dic requestIdentifier:1006 success:^(id result) {
        callback(result);
    }fail:^(id result) {
        failCallback(result);
    }];
}


- (void)updateUserPhoto:(void (^)(id))callback andFail:(void (^)(id))failCallback{
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[Common currentUserId] forKey:@"memberid"];
    [param setObject:_headImg forKey:@"headimg"];
    
    [self msg:@"imiheadimg" parameters:param requestIdentifier:1007 success:^(id result) {
        callback(result);
    }fail:^(id result) {
        failCallback(result);
    }];
}

- (void)getCityCode:(void (^)(id))callback {
    
    NSURL *url = [NSURL URLWithString:WEAHTERURL(@"027")];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        if (!error) {
            callback(dic);

        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"1");
    }];
    
    [operation start];
}
- (void)getUserInfo:(SIMPLE_BLOCK_MAKE(id result))callback andFail:(SIMPLE_BLOCK_MAKE(id result))failCallback
{
    
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[Common currentUserId] forKey:@"memberid"];
    
    [self msg:@"imimembercenter" parameters:param requestIdentifier:1008 success:^(id result) {
        callback(result);
    }fail:^(id result) {
        failCallback(result);
    }];
    
}

/*
 type	imisignup	需要请求的接口名称
 activeid	4       活动id
 memberid	9       活动id
 money      100     价格
 point      10      使用vb
 pay_price	90      实付金额
 name       李小拐   报名名称
 phone	13458582828	报名电话
 car_num	赣12306	报名车牌号
 num	1	参与人数
 remark	备注信息	备注信息
 */

- (void)activitysSignUp:(void (^)(id))callback andFail:(void (^)(id))failCallback {
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:[_info customObjectForkey:@"activeid"] forKey:@"activeid"];
    [param setObject:[_info customObjectForkey:@"memberid"] forKey:@"memberid"];
    [param setObject:[_info customObjectForkey:@"money"] forKey:@"money"];
    [param setObject:[_info customObjectForkey:@"point"] forKey:@"point"];
    [param setObject:[_info customObjectForkey:@"pay_price"] forKey:@"pay_price"];
    [param setObject:[_info customObjectForkey:@"name"] forKey:@"name"];
    [param setObject:[_info customObjectForkey:@"phone"] forKey:@"phone"];
    [param setObject:[_info customObjectForkey:@"car_num"] forKey:@"car_num"];
    [param setObject:[_info customObjectForkey:@"num"] forKey:@"num"];
    [param setObject:[_info customObjectForkey:@"remark"] forKey:@"remark"];
    
    [self msg:@"imisignup" parameters:param requestIdentifier:1009 success:^(id result) {
        callback(result);
    }fail:^(id result) {
        failCallback(result);
    }];
}

- (void)clickAgree:(void (^)(id))callback andFail:(void (^)(id))failCallback {
    //1:好评  0:差评
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:_orderId forKey:@"order_id"];
    [param setObject:_goodOrno forKey:@"goodorno"];
    
    [self msg:@"imigoodorerr" parameters:param requestIdentifier:1009 success:^(id result) {
        callback(result);
    }fail:^(id result) {
        failCallback(result);
    }];
}

- (void)getWeather:(void (^)(id))callback andFail:(void (^)(id))failCallback {
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:_city forKey:@"cityName"];

    [self  msg:@"imiweather" parameters:param requestIdentifier:3001 success:^(id result) {
        callback(result);
    }fail:^(id result) {
        failCallback(result);
    }];
}



@end
